package other.system.client.oa.submit;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.exception.AsyncTaskException;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.service.FutureTaskService;
import com.rkhd.platform.sdk.task.FutureTask;
import other.system.client.apitool.OAClientUtils;
import other.system.client.util.ResultBody;

/**
 * @Description: 异步提交OA附件
 * @Author: 黎炎松
 * @Date: 2024/9/24 14:00
 */
public class UploadOAFile implements FutureTask {

    private static final Logger logger = LoggerFactory.getLogger();

    public static void main(String[] args) {
        //String data = "{\"param\":\"[{\\\"name\\\":\\\"serve.jpg\\\",\\\"fileLength\\\":6077,\\\"url\\\":\\\"https://xsybucket.s3.cn-north-1.amazonaws.com.cn/3179016257966427/2024/09/05/b0f5bcd7-37ad-44da-be70-2605519e75b9.jpg\\\",\\\"fileId\\\":3453131878324910}]\",\"fdId\":\"192310bf393010119efdaa24c85afae7\",\"fdKey\":\"fd_37b206c7ec7a50\"}"
    }

    /**
     * 触发异步任务
     * @param data
     * @throws ScriptBusinessException
     */
    @Override
    public void execute(String data) throws ScriptBusinessException {
        logger.info("开始执行异步同步OA文件: " );
        logger.info(data);
        //OAClientUtils.sendPost(, OAClientUtils.UPLOAD_OA_FILE, true);
        //logger.info(LOGGER_MSG + "其他销售类更新附件resultBody: " + resultBody);
    }

    /**
     * 异步上传文件 （防止有的文件较大，执行时间过长问题）
     * @param oaId
     * @param fileKey
     * @param fileList
     */
    public static void syncUoloadFile(String oaId, String fileKey, JSONArray fileList) throws AsyncTaskException, ScriptBusinessException {
        logger.info("开始处理附件 ==> ");
        if (fileList == null || fileList.size() <= 0) {
            logger.info("没有附件，无需处理");
            return;
        }

        // 增加异步任务
        JSONObject sendData = new JSONObject();
        sendData.put("fdId", oaId);
        sendData.put("fdKey", fileKey);
        sendData.put("param", fileList.toJSONString());
        logger.info("sendData: " + sendData);

        ResultBody resultBody = OAClientUtils.sendPost(sendData, OAClientUtils.UPLOAD_OA_FILE, false);
        logger.info("更新OA审批附件: " + resultBody);

        // todo 线上环境调整为异步 （防止文件过大，超时问题）
        //new UploadOAFile().execute(sendData.toString());
        //String messageId = FutureTaskService.instance().addFutureTask(UploadOAFile.class, sendData.toString());
        //logger.info("上传OA附件异步ID:" + messageId);
    }

}
